package com.dz.Incubator.controller;

import com.dz.Incubator.entity.BillEntity;
import com.dz.Incubator.entity.InvokeResult;
import com.dz.Incubator.properties.oss.FileClient;
import com.dz.Incubator.properties.oss.file.OssFile;
import com.dz.Incubator.properties.oss.file.OssImage;
import com.dz.Incubator.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;

@RequestMapping("/api/upload")
@RestController
@Slf4j
public class UploadController {

    @Value(value = "${upload.dir}")
    private String filePath;

    @Value(value = "${upload.domain}")
    private String domain;


    @Value(value = "${aliyun.domain}")
    private String ossDomain;


    @Autowired
    private FileClient fileClient;


    @PostMapping(value = "upload")
    public InvokeResult upload(@RequestParam("file") MultipartFile file) {
        try {
            // 获取文件名
            String fileName = file.getOriginalFilename();
            // 获取文件的后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf(".")+1);
            // 根据当前时间生成新的文件名
            String newFileName = new Date().getTime() + suffixName;
            // 文件上传后的路径
            // 组装文件
//            File dest = new File(filePath + File.separator + newFileName);
//            // 检测是否存在目录
//            if (!dest.getParentFile().exists()) {
//                dest.getParentFile().mkdirs();
//            }
            // 将上传的文件写入到服务器端文件夹
//            file.transferTo(dest);
            //存OSS
            OssFile of = new OssImage();
            of.setStream(file.getInputStream());
            String location = fileClient.put(of,suffixName);
            // 返回
            System.err.println("【路径：】"+location);
            return InvokeResult.success(ossDomain + location);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping(value = "uploadBill")
    public InvokeResult uploadBill(@RequestParam("file") MultipartFile file) {
        try {
            BillEntity billEntity = new BillEntity();
            // 获取文件名
            String fileName = file.getOriginalFilename();
            billEntity.setBillName(fileName);
            // 获取文件的后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf(".")+1);
            // 根据当前时间生成新的文件名
//            String newFileName = new Date().getTime() + suffixName;
            // 文件上传后的路径
//            // 组装文件
//            File dest = new File(filePath + File.separator + newFileName);
//            // 检测是否存在目录
//            if (!dest.getParentFile().exists()) {
//                dest.getParentFile().mkdirs();
//            }
//            // 将上传的文件写入到服务器端文件夹
//            file.transferTo(dest);
            OssFile of = new OssFile(){};
            of.setStream(file.getInputStream());
            String location = fileClient.put(of,suffixName);
            billEntity.setBillUrl(ossDomain + location);
            return InvokeResult.success(billEntity);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("getDomain")
    public InvokeResult getDomain() {
        return InvokeResult.success(domain);
    }
}
